<?php
defined('IN_YuLin') || exit('NO PERMIT!');
echo '<pre>';
// 获取各类时时彩的的后台设置信息
$lottCateTab  = Table('lottery_category');
$lottTab      = Table('lottery');
$field        = 'cate.name as cate_name, cate.tags as cate_tags, lott.sname as lott_sname, lott.title as lott_name, lott.id as lott_id';
$sql          = 'SELECT ' . $field . ' FROM ' . $lottCateTab . ' AS cate
                LEFT JOIN ' . $lottTab . ' AS lott
                ON cate.`id` = lott.`cate_id`
                WHERE cate.`sname` = "ssc"';
$sscList = $db->getall($sql);
if (!$sscList) {
    echo '彩种信息获取失败！';
    return;
}
// 设置彩种补全信息
foreach ($sscList as $key => $value){
    switch ($value['lott_sname']){
        case 'cqssc':
            $sscList[$key]['total_by_day'] = '120';
            $sscList[$key]['begin_index']  = '24';
            $sscList[$key]['time_slot']    = array(
                '0' => array('slot'   => '09:50-21:50', 'secont' => 10 * 60, 's_total' => 72),
                '1' => array('slot'   => '21:50-01:45', 'secont' => 5 * 60, 's_total' => 48)
            );
            break;
        case 'xjssc':
            $sscList[$key]['total_by_day'] = '96';
            $sscList[$key]['begin_index']  = '1';
            $sscList[$key]['time_slot']    = array(
                '0' => array('slot'   => '09:58-01:58', 'secont' => 10 * 60, 's_total' => 96),
            );
            break;
        case 'tjssc':
            $sscList[$key]['total_by_day'] = '84';
            $sscList[$key]['begin_index']  = '1';
            $sscList[$key]['time_slot']    = array(
                '0' => array('slot'   => '08:55-22:55', 'secont' => 10 * 60, 's_total' => 84),
            );
            break;
//         case 'dfssc':
//             $sscList[$key]['total_by_day'] = '120';
//             $sscList[$key]['begin_index']  = '1';
//             $sscList[$key]['time_slot']    = array(
//                 '0' => array('slot'   => '09:00-23:00', 'secont' => 10 * 60, 's_total' => 120),
//             );
            break;
        default:
            break;
    }
}
$preTab      = Table('lottery_k3');

// 一次生成一年的期号，避免进入投注页面找不到期号
$tmpDay  = date('Y-m-d');
for ($di = 1; $di <= 60; $di++){
    $puDay   = date('Y-m-d', strtotime($tmpDay) + 86400);


    // 准备进行当天期号生成
    $baseDayTime   = str_replace('-', '', $tmpDay);
    $baseDayTime_1 = date('ymd', strtotime($tmpDay));
    foreach ($sscList as $k => $v){
        $checkCountSql  = 'SELECT COUNT(id) as cnt FROM ' . $preTab . ' WHERE tid = ' . $v['lott_id'] . ' AND `day` = "' . $baseDayTime . '"';
        $checkCountInfo = $db->getrow($checkCountSql);
        if ($checkCountInfo['cnt'] == $v['total_by_day']) continue;
        $index          = 0;
        $begin_index    = $v['begin_index'];
        if (!$v['time_slot']) continue;
        foreach ($v['time_slot'] as $tk => $tv){
            // 获取期号开始时间与结束时间戳
            $slotData  = explode('-', $tv['slot']);
            $btime     = strtotime($tmpDay . $slotData[0]);
            $etime     = '';
            if ($slotData[1] < $slotData[0]){
                // 第二天
                $etime = strtotime($puDay . $slotData[1]);
            }else{
                $etime = strtotime($tmpDay . $slotData[1]);
            }
            while ($etime >= $btime){
                if($index >= $v['total_by_day'] && $tk == (count($v['time_slot']) - 1)) break;
                $index++;
                if ($begin_index > $v['total_by_day']) $begin_index = 1;
                $qihao  = $baseDayTime_1 . str_pad( ($begin_index),3,0,STR_PAD_LEFT);
                $btime += $tv['secont'];
                // 基础期号累加
                $begin_index++;
    
                $ins = [];
                $ins['tid']		   = $v['lott_id'];
                $ins['period']	   = $qihao;
                $ins['starttime']  = $btime - $tv['secont'];
                $ins['stoptime']   = $btime;
                $ins['createtime'] = time();
                $ins['day']		   = $baseDayTime;
                $db->add('lottery_k3',$ins);
            }
        }
    }
    $tmpDay  = $puDay;
}
exit('success');




//重庆时时彩
$t2 = $db->getrow('SELECT COUNT(1) AS p FROM '.Table('lottery_k3').' WHERE tid = 11 AND day = \''.date('Ymd').'\'');
if($t2['p'] < 120)
{
	//	今天开始时间
	$dt = strtotime(date('Y-m-d'));
	for($i = 1; $i < 121; $i++)
	{
		$day = date('Ymd',$dt);
		if($i < 74)	//	10-22点总共73期,期号24开始
		{
			$stoptime = $dt + 10 * 3600 + ($i - 1) * 10 * 60;
			$starttime = $stoptime - 10 * 60;
			
			$qihao = date('ymd', $starttime).str_pad(($i + 23),3,0,STR_PAD_LEFT);
		}
		else if($i < 98)	//	22-24总共24期,期号97开始
		{
			$stoptime = $dt + 22 * 3600 + ($i - 73) * 5 * 60;
			$starttime = $stoptime - 5 * 60;
			
			$qihao = date('ymd', $starttime).str_pad(($i + 23),3,0,STR_PAD_LEFT);
		}
		else	//	0-2总共24期,期号1开始
		{
			$stoptime = $dt + 24 * 3600 + ($i - 97) * 5 * 60;
			$starttime = $stoptime - 5 * 60;
			
			$qihao = date('ymd', $starttime).str_pad(($i - 97),3,0,STR_PAD_LEFT);
		}
		
	    $ins = [];
	    $ins['tid']		=	'11';
	    $ins['period']	=	$qihao;
	    $ins['starttime']=	$starttime;
	    $ins['stoptime']=	$stoptime;
	    $ins['createtime']=	time();	
	    $ins['day']		=	$day;
		//print_r($ins);
		$db->add('lottery_k3',$ins);
	}
}

//天津时时彩
$t2 = $db->getrow('SELECT COUNT(1) AS p FROM '.Table('lottery_k3').' WHERE tid = 13 AND day = \''.date('Ymd').'\'');
if($t2['p'] < 83)
{
	//	今天开始时间
	$dt = strtotime(date('Y-m-d 08:55:00'));    // 9.00 是开奖时间， 这里手动校准首期时间
	$day = date('Ymd',$dt);

	for($i = 1; $i < 85; $i++)
	{
		$stoptime = $dt + $i * 10 * 60;
		$starttime = $stoptime - 10 * 60;
		
		$qihao = date('Ymd',$dt).str_pad($i,3,0,STR_PAD_LEFT);
		
		$t3 = $db->getrow('SELECT id FROM '.Table('lottery_k3').' WHERE tid = 13 AND period = \''.$qihao.'\'');
		if(!$t3)
		{
			$ins = [];
			$ins['tid']		=	'13';
			$ins['period']	=	$qihao;
		    $ins['starttime']=	$starttime;	
		    $ins['stoptime']=	$stoptime;		
		    $ins['createtime']=	time();	
		    $ins['day']		=	$day;

			$db->add('lottery_k3',$ins);
		}
	}
}

//新疆时时彩
$t2 = $db->getrow('SELECT COUNT(1) AS p FROM '.Table('lottery_k3').' WHERE tid = 12 AND day = \''.date('Ymd').'\'');
if($t2['p'] < 95)
{
	//	今天开始时间
	$dt = strtotime(date('Y-m-d 10:00:00'));
	for($i = 1; $i < 97; $i++)
	{
		$stoptime = $dt + $i * 10 * 60;
		$starttime = $stoptime - 10 * 60;
		$day = date('Ymd',$dt);
		
		$qihao = date('Ymd',$starttime).str_pad($i,2,0,STR_PAD_LEFT);
		
		$t3 = $db->getrow('SELECT id FROM '.Table('lottery_k3').' WHERE tid = 12 AND period = \''.$qihao.'\'');
		if(!$t3)
		{
			$ins = [];
			$ins['tid']		=	12;
			$ins['period']	=	$qihao;
		    $ins['starttime']=	$starttime;	
		    $ins['stoptime']=	$stoptime;		
		    $ins['createtime']=	time();	
		    $ins['day']		=	$day;
			$db->add('lottery_k3',$ins);
		}
	}
}
